package com.grape.action.base;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.grape.domain.base.Standard;
import com.grape.service.base.StandardService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller
@Namespace("/standard")
@Scope("prototype")
@ParentPackage("json-default")
public class StandardAction extends ActionSupport implements ModelDriven<Standard>{
	
	// 保存收派标准
	@Action(value="save" ,results=@Result(name="success",type="redirect",location="/pages/base/standard.html"))
	public String save() {
		System.out.println(ss);
		ss.save(standard);
		return SUCCESS;
	} 
	
	
	// 分页查询收派标准
	@Action(value="pagequery",results=@Result(name="success",type="json"))
	public String pageQuery() {
		Pageable pageable = new PageRequest(page-1, rows);
		Page<Standard> ps = ss.findAll(pageable);
		
		Map<String ,Object> mp = new HashMap<>();
		// 这里不要用element那个方法只展示一页，没有分页效果
		mp.put("total", ps.getTotalElements());
		mp.put("rows", ps.getContent());
		
		ServletActionContext.getContext().getValueStack().push(mp);
		
		return SUCCESS;
	}
	
	
	
	@Action(value="findAll",results=@Result(name="success",type="json"))
	public String findAll() {
		List<Standard> findAll = ss.findAll();
		ServletActionContext.getContext().getValueStack().push(findAll);
		
		return SUCCESS;
	}
	
	
	@Action(value="delBatch",results=@Result(name="success",type="json"))
	public String delBatch() {
		try {
			ss.delBatch(ids);
		} catch (Exception e) {
			ServletActionContext.getContext().getValueStack().push("正在使用中的收派标准");
		}
		
		return SUCCESS;
	}
	
	
	
	
	/*@Action(value="",results=@Result(name="success",location=""))
	public String demo() {
		
		return SUCCESS;
	}*/
	
	private String ids;
	public void setIds(String ids) {
		this.ids = ids;
	}







	private Standard standard = new Standard();
	@Autowired
	private StandardService ss;
	
	
	@Override
	public Standard getModel() {
		return standard;
	}
	
	
	private Integer page;
	private Integer rows;

	public void setPage(Integer page) {
		this.page = page;
	}
	public void setRows(Integer rows) {
		this.rows = rows;
	}
	
	

}
